package com.epalmpay.service.mall.impl;



import java.util.*;

import com.epalmpay.dto.bonus.BonusSendLogListOutDTO;
import com.epalmpay.dto.bonus.BonusSendLogOutDTO;
import com.epalmpay.dto.member.MemberOutDTO;
import com.epalmpay.entity.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.epalmpay.dto.mall.MainAdvertInDTO;
import com.epalmpay.dto.mall.MainAdvertOutDTO;
import com.epalmpay.entity.MainAdvert;
import com.epalmpay.mapper.MainAdvertMapper;
import com.epalmpay.service.mall.IMainAdvertService;
import com.epalmpay.util.PropertyCopyUtil;

/**
 * Created by gwn on 2017/5/22.
 */
@Service
public class MainAdvertServiceImpl implements IMainAdvertService{

	@Autowired
	private MainAdvertMapper mainAdvertMapper;

	
	@Override
	public List<MainAdvertOutDTO> listMainAdverts(MainAdvertInDTO mainAdvertOut,Page page) throws Exception {
		Map<String, Object> param = new HashMap<String, Object>();
		List<MainAdvertOutDTO> result = new ArrayList<MainAdvertOutDTO>();
		param.put("groupId", mainAdvertOut.getGroupId());
		if(mainAdvertOut.getAdvertType() !=null){
			param.put("advertType", mainAdvertOut.getAdvertType());
		}

		int sumCount = mainAdvertMapper.queryMainAdvertCount(param);
		page.setTotalResult(sumCount);
		if(sumCount<=0)
			return null;

		param.put("limit", page.getShowCount());
		param.put("start", page.getShowCount()*(page.getCurrentPage()-1));

		result =mainAdvertMapper.queryMainAdvertList(param);
		return result;


//		return mainAdvertMapper.queryMainAdvertList(mainAdvertOut);
	}

	
	@Override
	public void saveMainAdvert(MainAdvertInDTO mainAdvertInDTO) throws Exception {
		
		MainAdvert mainAdvert = new MainAdvert();
		PropertyCopyUtil.copyPropertiesExclude(mainAdvertInDTO,mainAdvert);
		
		mainAdvert.setAdvertName(mainAdvertInDTO.getAdvertName());
		mainAdvert.setPhotoUrl(mainAdvertInDTO.getPhotoUrl());
		mainAdvert.setClickUrl(mainAdvertInDTO.getClickUrl());
		mainAdvert.setStatus(mainAdvert.getStatus());
		mainAdvert.setSortNum(mainAdvert.getSortNum());
		mainAdvert.setGmtCreate(new Date());
		mainAdvert.setGmtModified(new Date());
		mainAdvert.setAdvertType(mainAdvertInDTO.getAdvertType());
		
		
		mainAdvertMapper.insertSelective(mainAdvert);
		return ;
	}


	@Override
	public void deleteMemberGrade(MainAdvert mainAdvert) {
		mainAdvertMapper.deleteByPrimaryKey(mainAdvert.getId());
	}


	@Override
	public MainAdvert findMainAdvertById(MainAdvert mainAdvert) throws Exception {
		
		return mainAdvertMapper.findMainAdvertById(mainAdvert.getId());
	}


	@Override
	public void editMainAdvert(MainAdvert mainAdvert) {
		mainAdvertMapper.updateByPrimaryKeySelective(mainAdvert);
	}

	/**
	 * 根据id查询状态
	 */
	@Override
	public MainAdvert findMainAdvert(Long id) {
		return mainAdvertMapper.findMainAdvert(id);
	}

	/**
	 * 跟新状态
	 */
	@Override
	public void updateStatus(MainAdvert mainAdverts) {
		
		if (mainAdverts.getStatus()==false) {
			mainAdverts.setStatus(true);
		}
		else{
			mainAdverts.setStatus(false);
		}
		mainAdvertMapper.updateByPrimaryKeySelective(mainAdverts);
		
	} 

}
